<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>


    <h1 id="showTime"></h1>

    <script>

        var oH1 = document.getElementById('showTime');

        oH1.innerHTML = formatDate() ;
        // formatDate()


        // 每隔一秒重新获取一下当前时间
        setInterval(function () {  
            oH1.innerHTML = formatDate() ;
            // formatDate()
        } , 1000) ;



        function addZero(n) {  
            return n < 10 ? '0' + n : n
        }


        function formatDate() {  
            var date = new Date();
            var year = date.getFullYear();
            var month = date.getMonth() + 1;   // 月份从0开始
            var day = date.getDate();   // 日期
            var week = date.getDay();  // 星期     星期天 0   0开始
            var hour = date.getHours();
            var minute = date.getMinutes();
            var second = date.getSeconds();
            var arr = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
            var res = `${year}-${month}-${day} ${hour}:${minute}:${second} ${arr[week]}`;
            // oH1.innerHTML = res ;
            return res ;
        }

    </script>

</body>

</html>